/**
 * AlertQueryAction.java
 * Created at 2017-11-2
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.RmsUtil;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.system.model.TtPackCustomAlert;
import com.broadtext.rms.system.service.IAlertQueryService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: AlertQueryAction</p>
 * <p>Description: 告警查询控制层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-2</p>
 */
@Controller
@RequestMapping("/main/alertQuery")
public class AlertQueryAction {
    
    
    /**
     * 区域服务类
     */
    @Autowired
    @Qualifier("alertQueryService")
    private IAlertQueryService alertQueryService;

    /**
     * 
     * <p>Description: 代码走一个差，主要是增加方法的具体描述人，增加一些</p>
     * @param request
     * @param response
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getAlertInfo.mvc")
    @ResponseBody
    public Map<String,Object> getCustomAlert(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        PageBounds pageBounds = new PageBounds();
        //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            EntityPageBean<TtPackCustomAlert> pageBean = this.alertQueryService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 查询电池型号下拉框</p>
     * @param request 请求
     * @return 结果
     */
    @RequestMapping(value = "/findPackModelOpt.mvc")
    @ResponseBody
    public Map<String, Object> findPackModelOpt (HttpServletRequest request) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.alertQueryService.findPackModelOpt());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: packCode模糊查询</p>
     * @param request 请求
     * @return 结果
     */
    @RequestMapping(value = "/getPackCodeList.mvc")
    @ResponseBody
    public Map<String, Object> getPackCodeList (HttpServletRequest request,String key) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.alertQueryService.getPackCodeList(key));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 导出历史数据</p>
     * @param packCode  packCode
     * @param startTime startTime
     * @param endTime  endTime
     * @param option  value="0">SOC  value="1">总电压  value="2">总电流 value="3">单体最高温度  value="4">单体温度与温差 value="5">单体电压与压差
     * @param response 响应
     * @throws IOException 异常
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/export.mvc")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServiceException{
      //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        /*
         * 构建动态map,进行excel导出  
         */
        String fileName = "告警数据";
        String title = "告警数据";
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        //构造方法 列head   列对应字段名      列宽
        entity.add(new ExcelExportEntity("电池ID", "packCode", 20));
        entity.add(new ExcelExportEntity("采集终端ID", "colClientId", 20));
        entity.add(new ExcelExportEntity("客户信息", "customerInfo", 30));
        entity.add(new ExcelExportEntity("告警名称", "alertName", 25));
        entity.add(new ExcelExportEntity("告警级别", "alertLevel", 15));
        //entity.add(new ExcelExportEntity("告警类型", "alertLevel", 15));
        entity.add(new ExcelExportEntity("电池系统型号", "packModel", 15));
        entity.add(new ExcelExportEntity("告警时间", "happenTimeStr", 25));
        
        /*
         * 封装查询条件进map
         */
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list = this.alertQueryService.findExportDataList(map);
        
        //调用导出方法进行excel导出
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(
                title, title), entity, list);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        workbook.write(bao);
        RmsUtil.exportExcel(fileName.concat(".xls"), response, bao);
        bao.close();
    }
}
